<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Money;
use App\Helper\Tool;
use App\Helper\Tree;
use App\Models\Area;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Http\Request;

class MoneyController extends AdminController
{

    /**
     * Make a grid builder.
     *
     */
    protected function grid()
    {
        $tab = Tab::make();
        $tab->add("充值设置",$this->money());
        $tab->add("充值说明",new \App\Admin\Forms\Config\Money());
        return $tab->withCard();
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function money()
    {
        return Grid::make(new Money(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('price');
            $grid->column('give_price');
            $grid->column('sort');
            $grid->column('status')->radio(['正常','隐藏'],true)->label(['info','default']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Money(), function (Show $show) {
            $show->field('id');
            $show->field('price');
            $show->field('give_price');
            $show->field('sort');
            $show->field('options');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Money(), function (Form $form) {
            $form->currency('price')->symbol("￥");
            $form->currency('give_price')->symbol("￥");
            $form->number('sort')->default(0);
            $cityList = Area::getOptions();
            $options = $form->model()->options?:[];
            $form->hidden("rule");
            $form->html(view('admin.money.rule',[
                'cityList'=>$cityList,
                'options'=>$options,
            ]),"设置规则");
            $form->radio('status')->options(['正常','隐藏'])->default(0);
            $form->saving(function (Form $form){
                $form->deleteInput('rule');
            });
        });
    }
}
